package GUI;

import java.io.File;
import java.io.IOException;
//import org.eclipse.jetty.client.ContentExchange;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;





public class ConnectToServer implements Runnable{
	public HttpClient client;
	public HttpPost httppost;
	//private String user;
	//private String password;
	
//	public ConnectToServer(String user,String password){
//		this.user = user;
//		this.password = password;
//	}
	
	@Override
	public void run() {
		httppost = new HttpPost("http://localhost:8080/connect");
		client = new DefaultHttpClient();
		try {
			client.execute(httppost);
			client.getConnectionManager().wait();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	/*	client = new HttpClient();
		//ContentExchange exchange = new ContentExchange(true);
	//	exchange.setURL("http://localhost:8080/connect");
		//"http://localhost:8080/connect";
	//	HttpPost httppost = new HttpPost("http://localhost:8080/connect");
	//	client = new DefaultHttpClient();
	//    client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
	//    client.execute(httppost);
	//    client.getConnectionManager().wait();
	   // exchange.
	  //  client.wait();

		client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
		try {
			client.start();
			ContentExchange exchange = new ContentExchange(true);
		//	exchange.addRequestHeader("user", user);
		//	exchange.addRequestHeader("password", password);
			exchange.setURL("http://localhost:8080/connect");
			client.send(exchange);
			exchange.waitForDone();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/ 
		
	}
	
	
	public void updateServer(String message){		
		client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

	    httppost = new HttpPost("http://localhost:8080/upload");
	    File file = new File("C:/dropbox/shay.txt");
	    
	    MultipartEntity mpEntity = new MultipartEntity();
	    ContentBody cbFile = new FileBody(file);
	    mpEntity.addPart("userfile", cbFile);


	    httppost.setEntity(mpEntity);
	    System.out.println("executing request " + httppost.getRequestLine());
	    HttpResponse response = null;
		try {
			response = client.execute(httppost);
			HttpEntity resEntity = response.getEntity();

		    System.out.println(response.getStatusLine());
		    if (resEntity != null) {
		      System.out.println(EntityUtils.toString(resEntity));
		    }
		    if (resEntity != null) {
		      resEntity.consumeContent();
		    }
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    

	   // client.getConnectionManager().shutdown();
	  }
}

	   
		
		
		//FileInputStream fileInputStream;
		//BufferedReader buf = null;// = null;
		//Object object = null; 
		
/*		try {
			File file = new File("C:/dropbox/shay.txt");
			byte[] b = new byte[(int)file.length()];
			//fileInputStream = new FileInputStream(file);
			//FileChannel channel = fileInputStream.getChannel();  
			ByteArrayBuffer byteBuffer = new ByteArrayBuffer(b);// = channel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());  
			ContentExchange exchange = new ContentExchange(true);
			//HttpBuffers buf = new HttpBuffers();
			
			exchange.setURL("http://localhost:8080/update");
			exchange.setRequestContentType("file");
			
			//exchange.setRequestContent((Buffer) byteBuffer);
	//		client.send(exchange);
			//exchange.ge
			exchange.waitForDone();
			System.out.println("send file");
			//fileInputStream.read(b);
			//buf = new BufferedReader(new InputStreamReader(fileInputStream));
			//BufferedInputStream buff = new BufferedInputStream(new ByteArrayInputStream(b));
			//object = new ObjectInputStream(new ByteArrayInputStream(b)).readObject();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
*/
		
		
		
		
	//    HttpPost httppost = new HttpPost("http://localhost:8080/update");
	    	    
	  //  MultipartEntity mpEntity = new MultipartEntity();
	//    ContentBody cbFile = new FileBody(file);
	  //  mpEntity.addPart("userfile", cbFile);


/*	    httppost.setEntity(mpEntity);
	    System.out.println("executing request " + httppost.getRequestLine());
	//    HttpResponse response = client.execute(httppost);
	    HttpEntity resEntity = response.getEntity();

	    System.out.println(response.getStatusLine());
	    if (resEntity != null) {
	      System.out.println(EntityUtils.toString(resEntity));
	    }
	    if (resEntity != null) {
	      resEntity.consumeContent();
	    }
*/
	
	
	
	

	
/*	public static String userName = "noahakim@gmail.com";
	public static String password = "12356";

	public static void connect(){
		HttpClient client = new HttpClient();
		client.getHostConfiguration().setHost("http://localhost:8080/");
		Socket socket = null;

	    try{
	      ConnectResponse response = client.
	      socket = response.getSocket();
	    }
	/*	client.getParams().setParameter("userName", userName);
	    client.getParams().setParameter("password", password);
	    BufferedReader br = null;
	    
	    PostMethod method = new PostMethod("http://localhost:8080/connectServer");
	    method.addParameter("userName", "password");
	    try{
	        int returnCode = client.executeMethod(method);

	        if(returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
	          System.err.println("The Post method is not implemented by this URI");
	          // still consume the response body
	          method.getResponseBodyAsString();
	        } else {
	          br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
	          String readLine;
	          while(((readLine = br.readLine()) != null)) {
	            System.err.println(readLine);
	          }
	        }
	      } catch (Exception e) {
	        System.err.println(e);
	      } finally {
	        method.releaseConnection();
	        if(br != null) try { br.close(); } catch (Exception fe) {}
	      }
		//	URLConnection connection = url.openConnection();
			//connection.setDoOutput(true);
			//connection.setRequestProperty(userName, userName);
			//connection.
			
		*/	
	
//	}


